package com.group01.community.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group01.community.entity.ZyOwner;
import com.group01.community.entity.ZyRepair;
import com.group01.community.entity.vo.ZyRepairVo;
import com.group01.community.service.ZyRepairService;
import com.group01.community.utils.ResponseResult;
import com.group01.community.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.time.LocalDateTime;

/**
 * <p>
 * 报修信息 前端控制器
 * </p>
 *
 * @author iuoly
 * @since 2022-07-04
 */
@Api(tags = "报修信息")
@RestController
@RequestMapping("/zyRepair")
public class ZyRepairController {

    @Autowired
    private ZyRepairService zyRepairService;

    @ApiOperation("查询所有报修信息")
    @GetMapping
    public ResponseResult<IPage<ZyRepairVo>> getZyRepairs(Long current, Long size, ZyRepair zyRepair, ZyOwner zyOwner) {

        IPage<ZyRepairVo> zyRepairVoIPage = zyRepairService.selectZyRepairPage(current, size, zyRepair, zyOwner);
        return ResponseResult.ok(zyRepairVoIPage);
    }

    /**
     * 小程序
     * - 新增报事报修
     * - 报事报修列表
     * - 报事报修进度追踪
     */
    @ApiOperation(value = "新增报修 " ,notes="注意 需要小区id 和user_id  ，user_id=业主id")
    @PostMapping("/add")
    public ResponseResult<String> addComplaintSuggest(@RequestBody ZyRepair zyRepair) {
        zyRepair.setRepairNum(UUIDUtils.getRepairCode());

        zyRepairService.save(zyRepair);
        return ResponseResult.ok("ok");
    }


//    @ApiOperation(" 报修 进度追踪" )
//    @GetMapping("/get")
//    public ResponseResult<String> getComplaintSuggestStatus() {
//
//
//        return ResponseResult.ok("ok");
//    }

    @ApiOperation("报事报修进度追踪")
    @PutMapping("/update")
    public ResponseResult<String> updateComplaintSuggestStatus(@RequestBody ZyRepair zyRepair) {

        if (zyRepair.getRepairState().equals("Cancelled")){
            zyRepair.setCancelTime(LocalDateTime.now());
        }
        if (zyRepair.getRepairState().equals("Processed")){
            zyRepair.setCompleteTime(LocalDateTime.now());
        }
        zyRepairService.updateById(zyRepair);
        return ResponseResult.ok("ok");
    }






}

